package com.ssi.util;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class HtmlUtil {
	public static String getAiTaoBaoUrl(String url){
		Document doc;
		String retUrl=null;
		try {
			doc = Jsoup.connect(url).get();
			Elements elementsByAttributeValue = doc.getElementsByAttributeValue("class", "featured-btn go-to-buy");
			if(elementsByAttributeValue.size()!=0){				
				retUrl = elementsByAttributeValue.get(0).attr("href");
				System.out.println(retUrl);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("获取商品爱淘宝地址出现异常！");
		}
		return retUrl;
		
	}
	public static String getTaoBaoUrlByAiTao(String url){
		Document doc;
		String retUrl=null;
		try {
			doc = Jsoup.connect(url).get();
			retUrl = doc.baseUri();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("获取商品淘宝地址出现异常！");
		}
		return retUrl;
		
		
	}
	public static String getTaoBaoUrl(String url){
		String aiTaoBaoUrl = getAiTaoBaoUrl(url);
		String taoBaoUrl = getTaoBaoUrlByAiTao(aiTaoBaoUrl);
		return taoBaoUrl;
	}
	public static void main(String[] args) throws IOException {
		String url="http://s.click.taobao.com/t_js?tu=http%3A%2F%2Fs.click.taobao.com%2Ft%3Fe%3Dm%253D2%2526s%253DUVUk4AHsyGkcQipKwQzePDAVflQIoZepLKpWJ%252Bin0XJRAdhuF14FMdlb66mLKdj9t4hWD5k2kjOXxXwr7ajD271AdmGlj2tcrS2tF%252FEKoyPBCYirjHB%252B0pV5xOrTn%252BqL%26ref%3D%26et%3D%252BtZFC68ed%252FI3HCxNfMRGBO7clHIt2Uuy";
		String aiTaoBaoUrl = getAiTaoBaoUrl(url);
		//String taoBaoUrl = getTaoBaoUrlByAiTao(aiTaoBaoUrl);
		System.out.println(aiTaoBaoUrl);
		//System.out.println(taoBaoUrl);
		
		/**
		Document doc = Jsoup.connect("http://ai.taobao.com/auction/edetail.htm?e=2q0NCHti6OjebLdhAWchHHnxuOLFzmmYWDbyJyU0aqSLltG5xFicOdXrTUTgh9sMDPIwxrc30riPbH9cdIJzx%2FleSgdHUZJlWsdGmyHpgVpm0JFKVvBge%2BIZWR1bMnHu&unid=53589436&ptype=100010&from=basic&clk1=7eb310655e345d81f95a73e6ba799f78&upsid=7eb310655e345d81f95a73e6ba799f78").get();
		//System.out.println(doc.toString());
		Elements elementsByAttributeValue = doc.getElementsByAttributeValue("class", "featured-btn go-to-buy");
		Elements elementsByClass = doc.getElementsByClass("right-part");
		
		Elements elementsByClass2 = doc.getElementsByClass("featured-btn"); 
		Elements elementsByClass3 = doc.getElementsByClass("go-to-buy");
		Elements elementsByClass4 = doc.getElementsByClass("featured-btn:go-to-buy");
		System.out.println(elementsByAttributeValue);
		System.out.println("------------------------");
		System.out.println(elementsByClass2);
		System.out.println("------------------------");
		System.out.println(elementsByClass3);
		System.out.println("------------------------");
		System.out.println(elementsByClass4);
		System.out.println("------------------------");
		String attr2 = elementsByAttributeValue.attr("href");
		System.out.println(attr2);
		**/
	
	}
}
